<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use think\Session;
use think\Db;

class Login extends Controller
{
   public function login(Request $request){
       if( $request->isPost() ){
           /*利用强制转换验证提交的字段类型*/
           $username = (string)$request->post('username');
           $password = (string)$request->post('password');
           $code = $request->post('code');
           /*根据用户名查询出数据库里的用户密码*/
           $password_sql_tmp = Db::name('user')->where('username',"$username")->find();
           $password_sql =  $password_sql_tmp['password'];
           $id = $password_sql_tmp['id'];
           /*密码验证通过的话*/
           if( $password_sql === md5($password) ){
               /*处理用户名  写入session*/
               $username =  $password_sql_tmp['username'];
               Session::set('username',$username);
               /*登录成功，跳转*/
               $this->redirect('/admin/index/index');
           }else{
               $this->error('密码错误！','/admin/login/login');
           }
       }else{
           return $this->fetch();
       }
   }


   public function logout(){
       Session::clear();
       $this->redirect('/admin/login/login');
   }
}